<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/' }">菜品</el-breadcrumb-item>
      <el-breadcrumb-item>列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 白色内容面板区 -->
    <div class="page_content">
      <!-- 新增菜品按钮 -->
      <div class="input_box">
        <el-button type="primary" @click="link('/food/create')">新增菜品</el-button>
      </div>
      <!-- 表格 -->
      <el-table :data="page.records" border>
        <!-- 菜品图片 -->
        <el-table-column
          prop="icon"
          label="菜品图片"
          width="180"
          align="center"
        >
          <template slot-scope="scope">
            <img :src="scope.row.icon" width="50" height="50" />
          </template>
        </el-table-column>
        <!-- 菜名 -->
        <el-table-column prop="name" label="菜名" width="180" align="center">
        </el-table-column>
        <!-- 菜系 -->
        <el-table-column prop="type" label="菜系" width="180" align="center">
        </el-table-column>
        <!-- 价格 -->
        <el-table-column prop="price" label="价格" width="180" align="center">
          <template slot-scope="props"> {{ props.row.price }}元 </template>
        </el-table-column>
        <!-- 推荐 -->
        <el-table-column
          prop="recommend"
          label="推荐"
          width="180"
          align="center"
        >
          <template slot-scope="props">
            <el-switch
              v-model="props.row.recommend"
              @change="changeRecommend(props.row.id)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" align="right">
          <template>
            <el-button type="primary">Edit</el-button>
            <el-button type="danger"> <i class="el-icon-delete"></i></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        class="flex_left"
        background
        layout="prev, pager, next"
        :total="page.total"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { list, updateRecommend } from "@/util/request/foodApi";

export default {
  name: "FoodList",
  data() {
    return {
      // 分页数据
      page: {
        records: [],
        total: 1,
        current: 1,
      },
    };
  },

  methods: {
    // 显示或者搜索菜品
    searchList(val) {
      list({ current: val }).then((res) => {
        if (res.data) {
          this.page = res.data.data;
          for (let i = 0; i < res.data.data.records.length; i++) {
            this.page.records[i].recommend =
              res.data.data.records[i].recommend == "1" ? true : false;
          }
          console.log("菜品数据", this.page);
        }
      });
    },

    // 修改推荐
    changeRecommend(val) {
      updateRecommend(val).then((res) => {
        console.log(res);
      });
      console.log(val);
    },

    // 换页
    handleCurrentChange(val) {
      this.searchList(val);
    },

    // 重定向
    link(path) {
      this.$router.push({
        path,
      });
    },
  },

  mounted() {
    this.searchList();
  },
};
</script>

<style>
</style>